<?php
namespace app\admin\controller;

use app\core\Incontroller;
use think\Request;

class Auth extends Incontroller
{
    public function _empty($name){
        return $name;
    }

    public function index(){

        $auth_mod = model('Auth');
        $auth = $auth_mod->getAll();
        $this->assign('auth',$auth);
        return $this->fetch('auth/index');
    }

    public function add(){  //添加
        if(isset($_POST['__action_type__']) && $_POST['__action_type__']=='save'){
            $res = $this->_save();
            if($res){
                return ['err'=>0,'msg'=>'添加成功'];
            }else{
                return ['err'=>1,'msg'=>'添加失败'];
            }
        }

        $auth_mod = model('Auth');
        $res = $auth_mod->where('pid=0')->select();
        $this->assign('auth',$res);
        return $this->fetch('auth/add');
    }


    protected function _save(){
        $data = array(
            'name'        => $_POST['name'],
            'css_icon'    => $_POST['css_icon'],
            'module'      => 'admin',
            'controller'  => $_POST['controller'],
            'action'      => $_POST['action'],
            'pid'         => $_POST['pid'],
            'is_menu'     => $_POST['is_menu'],
            'menu_level'  => $_POST['menu_level'],
            'weight'      => $_POST['weight'],
            'create_time' => date("Y-m-d H:i:s"),
            'update_time' => date("Y-m-d H:i:s")
        );
        $auth_mod = model('Auth');
        if(isset($_POST['id']) && $_POST['id']){
            $res = $auth_mod->save($data,['id'=>$_POST['id']]);
        }else{
            $res = $auth_mod->save($data);
        }
        return $res;

    }
}